import {takeEvery,put} from 'redux-saga/effects'
import {Get_INIT_LIST} from './actionTypes'
import {initListAction} from './actionCreator'

import axios from 'axios'

//引入saga之后dispatch的action不仅可以在reducer做逻辑，还可以在sagas.js
function* mySaga() {
    //Generator 函数的语法
    yield takeEvery(Get_INIT_LIST, getInitList);
}

function* getInitList(){
    //console.log('abc')
    //他会等待ajax获取完毕直接存到res里面
    try {  

        const res = yield axios.get('data.json');  
        const action = initListAction(res.data)
        yield put(action);//等action执行完在执行

    } catch (error) {
        console.log(error)
       // console.log("error")
    }
   

}
 
export default mySaga;